package com.trainsmart.smartmvc
{
	import com.trainsmart.smartmvc.conf.Property;
	
	import flash.utils.getDefinitionByName;
	
	import mx.containers.VBox;

	public class SmartForm extends VBox
	{
		private var _properties:Array = null;
		private var  _vo:Object = new Object();
		private var _context:Context = null;
		
		
		public var viewId:String;
		
		public function SmartForm()
		{
			super();
		
		}
		
		public function init(context:Context, viewId:String, properties:Array, vo:String=null):void{
			_context = context;			
			this.viewId = viewId;
			_properties = properties;
			if(vo!=null){
				var VOClass:Class = getDefinitionByName(vo) as Class;
				_vo = new VOClass();
			}
		}
		
		
		
		/**
		 * show value from configure to UI
		 */ 
		public function show():void{
			for(var i:int=0;i<_properties.length;i++){
				var p:Property = _properties[i] as Property;			
				this[p.component][p.attribute]=_vo[p.field];
				
			}
		}
		/**
		 * show value from vo to UI
		 */ 
		override public function set data(vo:Object):void{
			if(vo==null){
				for(var i:int=0;i<_properties.length;i++){
					var p:Property = _properties[i] as Property;
					try{			
						this[p.component][p.attribute] = '';
					}catch(e:Error){
						
					}
				}
			}else{
				
				for(var i:int=0;i<_properties.length;i++){
					var p:Property = _properties[i] as Property;			
					this[p.component][p.attribute] = vo[p.field];				
				}		
			}
		}
		
		/**
		 * get value from UI to vo;
		 */ 
		override public function get data():Object{
			
			for(var i:int=0;i<_properties.length;i++){
				var p:Property = _properties[i] as Property;			
				_vo[p.field] = this[p.component][p.attribute];
			}
			return _vo;
		}
		
		public function execute(preViewId:String, response:Object=null, response:Function=null):void
		{
				trace(preViewId+" -- "+ response);
		}
		
	}
}